// 包含应用中所有请求接口的函数：接口请求函数
import ajax from "./ajax";
const BASE = "http://121.36.240.69:5000";

export const reqLogin = (username, password) => {
  return ajax({
    method: "post",
    url: BASE + "/login",
    data: {
      // 默认使用json格式的请求体挈带参数数据
      username,
      password,
    },
  });
};

// 获取分类列表
export const reqCategorys = () => {
  return ajax(BASE + "/manage/category/list");
};
// 修改分类
export const reqUpdateCategory = ({ categoryId, categoryName }) => {
  return ajax.post(BASE + "/manage/category/update", {
    categoryId,
    categoryName,
  });
};
// 添加分类
export const reqAddCategory = ({ categoryName }) => {
  return ajax.post(BASE + "/manage/category/add", {
    categoryName,
  });
};

// 删除分类
export const reqDeleteCategory = (categoryId) => {
  return ajax.post(BASE + "/manage/category/delete", {
    categoryId,
  });
};

// 获取所有的商品
export const reqProducts = (pageNum, pageSize) => {
  return ajax(BASE + "/manage/product/list", {
    params: {
      pageNum,
      pageSize,
    },
  });
};
// 根据搜索关键字 获取商品分页列表
// 根据商品名称搜搜
// 根据描述搜索
// /manage/product/search?pageNum=1&pageSize=4&productName=V
export const reqSearchProducts = ({
  pageNum,
  pageSize,
  searchType,
  searchName,
}) => {
  return ajax(BASE + "/manage/product/search", {
    params: {
      pageNum,
      pageSize,
      [searchType]: searchName,
    },
  });
};

// 根据商品Id获取商品详情
export const reqProduct = (productId) => {
  return ajax(BASE + "/manage/product/info", {
    params: {
      productId,
    },
  });
};

// 根据分类Id获取商品分类的名称
export const reqCategory = (categoryId) => {
  return ajax(BASE + "/manage/category/info", {
    params: {
      categoryId,
    },
  });
};

// 添加或者是更改商品
export const reqAddOrUpdateProduct = (product) => {
  return ajax.post(
    `${BASE}/manage/product/${product._id ? "update" : "add"}`,
    product
  );
};

// 更新产品状态接口
export const reqUpdateProductStatus = async (productId, status) => {
  try {
    const response = await ajax.post(`${BASE}/manage/product/updateStatus`, {
      productId,
      status,
    });
    console.log("response:", response);
    return response;
  } catch (error) {
    console.error("更新产品状态失败:", error);
    throw error;
  }
};

// 删除产品接口
export const reqDeleteProduct = async (productId) => {
  try {
    const response = await ajax.post(`${BASE}/manage/product/delete`, {
      productId,
    });
    return response;
  } catch (error) {
    console.error("删除产品失败:", error);
    throw error;
  }
};

// 获取所有的用户列表
export const reqUsers = () => {
  return ajax(BASE + "/manage/user/list");
};

// 添加或者更新当前用户
export const reqAddOrUpdateUser = (user) => {
  return ajax.post(`${BASE}/manage/user/${user._id ? "update" : "add"}`, user);
};

// 删除指定的用户
export const reqRemoveUser = (userId) => {
  return ajax.post(BASE + "/manage/user/delete", {
    userId,
  });
};

// 获取角色列表
export const reqRoles = () => {
  return ajax(BASE + "/manage/role/list");
};

// 添加角色
export const reqAddRole = (roleName) => {
  return ajax.post(BASE + "/manage/role/add", {
    roleName,
  });
};

// 删除角色
export const reqDeleteRole = (roleId) =>
  ajax.post(`${BASE}/manage/role/delete`, { roleId });

// 设置角色权限
export const reqAddRoleAuth = (role) => {
  return ajax.post(BASE + "/manage/role/update", role);
};
